﻿using Pb;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NNCardsControl : MonoBehaviour
{
    EginDN dn = EginDN.Instance;
    EginUser user = EginUser.Instance;
    public List<int> ownCards; //自己的牌
    private List<int> sortOwnCards;
    public List<NNCards> nnCards; //所有人的手牌
    public GameObject ShowCardBtn; //开牌按钮
    int CardType; //牌类型

    private GameObject OwnCardType;
    private void Awake()
    {
        UIEventListener.Get(ShowCardBtn).onClick = ButtonClick;

    }

    void ButtonClick(GameObject button)
    {
        if (button.name == "ShowCardBtn")
        {
            SendShowCard();
            ShowCardBtn.SetActive(false);
        }
    }
    /// <summary>
    /// 第一轮显示自己的牌最后一张牌不显示
    /// </summary>
    /// <param name="Cards"></param>
    /// <param name="seatID"></param>
    public void ShowOwnCards(List<int> Cards, int seatID, int PokerType)
    {
        //ownCards = Cards;
        for (int i = 0; i < 5; i++)
        {
            int random = Random.Range(0, 4);
            int temp1 = Cards[0];
            int temp2 = Cards[random];
            Cards[random] = temp1;
            Cards[0] = temp2;
        }
        sortOwnCards = Cards;
        CardType = PokerType;
        //判断有哪些玩家 显示有玩家的牌
        ShowPlayerCards();
        //最后一张牌不显示 所以nnCards[0].cardsSprit.Count-1
        for (int i = 0; i < nnCards[0].cardsSprit.Count - 1; i++)
        {
            nnCards[0].cardsSprit[i].spriteName = Cards[i].ToString();
        }
        nnCards[0].cardsSprit[4].spriteName = "0";
        nnCards[0].SeatID = seatID;
    }
    /// <summary>
    /// 判断有哪些玩家  显示有玩家的牌
    /// </summary>
    void ShowPlayerCards()
    {
        for (int i = 0; i < user.gameUserInfoList.Count; i++)
        {
            int tempIndex = dn.returnOtherSeatIndex(user.gameUserInfoList[i].SeatId);
            for (int j = 0; j < 5; j++)
            {
                nnCards[tempIndex].cardsSprit[j].gameObject.SetActive(true);
            }
        }
    }


    /// <summary>
    /// 第三人加入要调用
    /// </summary>
    public void OtherJoinShowCard()
    {
        for (int i = 0; i < user.gameUserInfoList.Count; i++)
        {
            if (!user.gameUserInfoList[i].IsWitness)
            {
                int tempIndex = dn.returnOtherSeatIndex(user.gameUserInfoList[i].SeatId);
                for (int j = 0; j < 5; j++)
                {
                    nnCards[tempIndex].cardsSprit[j].gameObject.SetActive(true);
                }
            }
        }
    }
    /// <summary>
    /// 重连在游戏中调用
    /// </summary>
    public void ReconnectShowCard()
    {
        for (int i = 0; i < user.gameUserInfoList.Count; i++)
        {
            if (!user.gameUserInfoList[i].IsWitness)
            {
                int tempIndex = dn.returnOtherSeatIndex(user.gameUserInfoList[i].SeatId);
                for (int j = 0; j < 5; j++)
                {
                    nnCards[tempIndex].cardsSprit[j].gameObject.SetActive(true);
                }
            }
        }
    }


    /// <summary>
    /// 显示庄家最后一张牌
    /// </summary>
    public void ShowBankerLastCard()
    {

        nnCards[0].cardsSprit[4].spriteName = sortOwnCards[4].ToString();
        ShowCardBtn.SetActive(true);
        ShowOwnCardType();
    }
    /// <summary>
    /// 显示玩家最后一张牌
    /// </summary>
    public void ShowPlayerLastCard()
    {
        nnCards[0].cardsSprit[4].spriteName = sortOwnCards[4].ToString();
        ShowCardBtn.SetActive(true);
        ShowOwnCardType();
    }
    /// <summary>
    /// 显示自己的牌型 加载预设
    /// </summary>
    void ShowOwnCardType()
    {
        OwnCardType = LoadModelReturnObj(GameDNPrafabPath.OwnCardType, gameObject.transform);
        NNOwnCardType nnOwnCardType = OwnCardType.GetComponent<NNOwnCardType>();
        nnOwnCardType.CardType = CardType;
        nnOwnCardType.ownCards = ownCards;
    }



    /// <summary>
    /// 发送NnPlayRequest
    /// </summary>
    void SendShowCard()
    {
        NnPlayRequest lg = new Pb.NnPlayRequest { };
        SocketManager socketManager = SocketManager.Instance;
        socketManager.SendProtobuf1(lg, MsgId.NnPlayRequest);
    }
    /// <summary>
    /// 开牌  设置牛的位置
    /// </summary>
    public void ShowCard(List<int> cardList, int pokerType, int SeatID)
    {
        if (SeatID == dn.seatId)
        {
            Destroy(OwnCardType);
            ShowCardBtn.SetActive(false);
        }

        int tempIndex = dn.returnOtherSeatIndex(SeatID);
        for (int i = 0; i < nnCards[tempIndex].cardsSprit.Count; i++)
        {
            nnCards[tempIndex].cardsSprit[i].spriteName = cardList[i].ToString();
        }
        GameObject obj = null;
        if (pokerType == 0)
        {
            obj = LoadModelReturnObj(GameDNPrafabPath.NoCow, nnCards[tempIndex].gameObject.transform);
        }
        else if (pokerType > 0 && pokerType <= 10)
        {
            obj = LoadModelReturnObj(GameDNPrafabPath.HaveCow, nnCards[tempIndex].gameObject.transform);
            NNCardType nct = obj.GetComponent<NNCardType>();
            nct.cardSprit.spriteName = pokerType.ToString();
        }
        else if (pokerType == 11)
        {
            obj = LoadModelReturnObj(GameDNPrafabPath.SiZha, nnCards[tempIndex].gameObject.transform);
        }
        else if (pokerType == 12)
        {
            obj = LoadModelReturnObj(GameDNPrafabPath.WuHuaNiu, nnCards[tempIndex].gameObject.transform);
        }
        else if (pokerType == 13)
        {
            obj = LoadModelReturnObj(GameDNPrafabPath.WuXiaoNiu, nnCards[tempIndex].gameObject.transform);
        }

        if (tempIndex == 0)
        {
            obj.transform.localPosition = new Vector3(0, -39, 0);
        }
        else if (0 < tempIndex && tempIndex < 4)
        {
            obj.transform.localScale = Vector3.one * 0.7f;
            obj.transform.localPosition = new Vector3(-200, -15, 0);
        }
        else if (tempIndex == 4)
        {

            obj.transform.localScale = Vector3.one * 0.7f;
            obj.transform.localPosition = new Vector3(-96, 15, 0);
        }
        else if (tempIndex == 5)
        {
          
            obj.transform.localScale = Vector3.one * 0.7f;
            obj.transform.localPosition = new Vector3(-350, 15, 0);
        }
        else if (tempIndex == 6)
        {
            obj.transform.localScale = Vector3.one * 0.7f;
            obj.transform.localPosition = new Vector3(-583, 15, 0);
        }
        else
        {
            obj.transform.localScale = Vector3.one * 0.7f;
            obj.transform.localPosition = new Vector3(-172, -15, 0);
        }
    }





    /// <summary>
    /// 加载预设
    /// </summary>
    /// <param name="TempPath"></param>
    /// <param name="fatherTrans"></param>
    /// <returns></returns>
    GameObject LoadModelReturnObj(string TempPath, Transform fatherTrans)  // fatherTrans 深度统一
    {
        Object obj = Resources.Load(TempPath, typeof(GameObject));
        GameObject go = Instantiate(obj, transform.position, transform.rotation, fatherTrans) as GameObject;
        return go;
    }

}
